// pages/purchaseHall/purchaseFriend/myapplyDetails.js

import api from '../../../utils/api.js';//这个里面封装了request方法
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // yijia:false,//议价：改变
    hideshowFormOfferPopup:true,
    formId:'',
    formoffers:[{
      formId:'',
      saleuserId:'',
      saleusername:'hj',
      salehead:"",
      count:'11',
      price:'90',
      fromAddr:'湖南长沙',
      status:0
    }],
    formoffer:{ 
    formId:'',
    saleuserId:'',
    saleusername:'Admin',
    salehead:"",
    count:'11',
    price:'2.1',
    fromAddr:'湖南长沙',
   status:0,
},
    form:{
      "formId": "1",
      "userId": "1",
      "catId": "1",
      "itemId": "1",
      "cycle": "day",
      "count": 2000,
      "receiptAddress": "湖南",
      "expectedAddress": "河北",
      "price": 2.0,
      "description": null,
      "offercount": 1,
      "status": 0,
      "units": "斤",
      "ddl": "2021-07-15",
      "totalcount": 2000,
      "itemName": "西瓜",
      "categoryId": "1",
      "image1": "https://dcdn.it120.cc/2021/07/15/b1763660-78bd-4a2d-bbae-9e68dc76bdbc.png",
      "image2": null
  },
    createTabs: true, //绘制tabs
    tabs: [{
      tabs_name: '采购商品',
      view_id: 'swiper-container',
      topHeight: 0
    }, {
      tabs_name: '采购详情',
      view_id: 'goods-des-info',
      topHeight: 0,
    },
    {
      tabs_name: '接单情况',
      view_id: 'goods-des-info',
      topHeight: 0,
    }],
    formoffersIndex:10
  },
  showFormOffer(event){
    console.log("showFormOffer");
    console.log(event.currentTarget.id)

    let temp = this.data.formoffers[event.currentTarget.id]
    console.log(temp)
    this.setData({hideshowFormOfferPopup:false,
    formoffer:temp})

  },
  closePopupTap(){
    this.setData({hideshowFormOfferPopup:true})
  },
  bindscroll(e) {
    //计算页面 轮播图、详情、评价(砍价)view 高度
    this.getTopHeightFunction()
    var tabsHeight = this.data.tabsHeight //顶部距离（tabs高度）
    if (this.data.tabs[0].topHeight-tabsHeight<=0 && 0 < this.data.tabs[1].topHeight-tabsHeight) { //临界值，根据自己的需求来调整
      this.setData({
        active: this.data.tabs[0].tabs_name //设置当前标签栏
      })
    } else if (this.data.tabs[1].topHeight-tabsHeight<=0) {
      this.setData({
        active: this.data.tabs[1].tabs_name
      })
    }
  },
  getTopHeightFunction() {
    var that = this
    var tabs = that.data.tabs
    tabs.forEach((element, index) => {
      var viewId = "#" + element.view_id
      that.getTopHeight(viewId, index)
    });
  },
  getTopHeight(viewId, index) {
    var query = wx.createSelectorQuery();
    query.select(viewId).boundingClientRect((rect) => {
      if (!rect) {
        return
      }
      let top = rect.top
      var tabs = this.data.tabs
      tabs[index].topHeight = top
      this.setData({
        tabs: tabs
      })
    }).exec()
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("onload")
    this.setData({formId:options.formId});
    //初始化form数据
    const url = 'forms/'+this.data.formId;
    api.get(url,{
      data:''
    }).then(res => {
      console.log("11");
     //得到的数据
     console.log(res)
     this.setData({form:res.data})
     var limit = this.data.form.totalcount * 0.2
     let formIdTemp = this.data.form.formId
     this.setData({ buyNumber:limit,
                    formId:formIdTemp
           })
      console.log(this.data.formId)
     console.log(this.data.form);
     wx.showToast({
       title: '加载成功',
       icon: 'none'
     })
   }).catch(err => {
     console.log(err)
     wx.showToast({
       title: "请求错误",
       icon: 'none'
     })
   })
    

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})